package com.sinosoft.perfect.system.license.service;

import java.sql.SQLException;
import java.util.List;

import com.sinosoft.perfect.system._.util.Producer;
import com.sinosoft.perfect.system.license.bean.LicenseContainerBean;
import com.sinosoft.perfect.system.license.dao.ILicenseDataDAO;
import com.sinosoft.perfect.system.license.vo.License;

public class LicenseDataServiceImpl implements LicenseDataService {
	
	private ILicenseDataDAO licenseDataDao;
	private LicenseContainerBean licenseContainerBean;
	
	public LicenseContainerBean getLicenseContainerBean() {
		return licenseContainerBean;
	}
	public void setLicenseContainerBean(LicenseContainerBean licenseContainerBean) {
		this.licenseContainerBean = licenseContainerBean;
	}
	public ILicenseDataDAO getLicenseDataDao() {
		return licenseDataDao;
	}

	public void setLicenseDataDao(ILicenseDataDAO licenseDataDao) {
		this.licenseDataDao = licenseDataDao;
	}

	
	@Override
	public License initLicense(String licenseMechine) throws SQLException {
		License license = initLicense();
		if(license == null){
			if(licenseMechine != null && licenseMechine.length()>10){
				license = new License();
				license.setLicenseId(Producer.generateIdOfTen());
				license.setLicenseMechine(licenseMechine);
				licenseDataDao.insertLicense(license);
				license = initLicense();
			}
		}
		return license;
	}

	
	@Override
	public License initLicense() throws SQLException {
		List<License> licenses = licenseDataDao.selectLicense();
		License license = null;
		if(licenses != null && licenses.size()>0){
			license = licenses.get(0);
		}
		return license;
	}

	@Override
	public String getLicenseStatus() {
		return licenseContainerBean.getLicenseStatus();
	}
	
	@Override
	public String registerLicense(License license) {
		String registerResult = "ok";
		License regesterLicense = licenseContainerBean.validateRegisterLicense(license);
		if(regesterLicense != null)
			try {
				licenseDataDao.updateLicense(regesterLicense);
				licenseContainerBean.setLicense(regesterLicense);
			} catch (SQLException e) {
				e.printStackTrace();
				registerResult = "regesterError";
			}
		else
			registerResult = "licenseError";
		
		return registerResult;
	}
	
	@Override
	public License initLicenseForShow() throws SQLException {
		License license = this.initLicense();
		if(license != null){
			license.setLicenseStart(licenseContainerBean.getLicenseStartDecoding());
			license.setLicenseEnd(licenseContainerBean.getLicenseEndDEcoding());
		}
		return license;
	}
	@Override
	public String deleteLicense(License license) throws SQLException {
		String flag = "no";
		License license2 = licenseContainerBean.validateRegisterLicense(license);
		license2.setLicenseEnd("");
		license2.setLicenseStart("");
		license2.setLicenseSerial("");
		license2.setLicenseName("");
		try {
			licenseDataDao.updateLicense(license2);
			licenseContainerBean.setLicense(license2);
			flag = "ok";
		} catch (SQLException e) {
			e.printStackTrace();
			flag = "error";
		}
		return flag;
	}
	
	
}
